package com.springcloud.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.springcloud.commons.result.Tree;
import com.springcloud.mapper.SysOrganizationMapper;
import com.springcloud.model.SysOrganization;
import com.springcloud.service.ISysOrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * SysOrganization 表数据服务层接口实现类
 *
 */
@Service
public class SysOrganizationServiceImpl extends ServiceImpl<SysOrganizationMapper, SysOrganization> implements ISysOrganizationService {

	 @Autowired
	    private SysOrganizationMapper organizationMapper;

	    @Override
	    public List<Tree> selectTree() {
	        List<Tree> trees = new ArrayList<Tree>();
	        //查询一级菜单
	        List<SysOrganization> organizationFather = organizationMapper.selectByPIdNull();

	        if (organizationFather != null) {
	            for (SysOrganization organizationOne : organizationFather) {
	                Tree treeOne = new Tree();

	                treeOne.setId(organizationOne.getId());
	                treeOne.setText(organizationOne.getName());
	                treeOne.setIconCls(organizationOne.getIcon());
	                //查询子菜单
	                List<SysOrganization> organizationSon = organizationMapper.selectAllByPId(organizationOne.getId());

	                if (organizationSon != null) {
	                    List<Tree> tree = new ArrayList<Tree>();
	                    for (SysOrganization organizationTwo : organizationSon) {
	                        Tree treeTwo = new Tree();
	                        treeTwo.setId(organizationTwo.getId());
	                        treeTwo.setText(organizationTwo.getName());
	                        treeTwo.setIconCls(organizationTwo.getIcon());
	                        tree.add(treeTwo);
	                    }
	                    treeOne.setChildren(tree);
	                } else {
	                    treeOne.setState("closed");
	                }
	                trees.add(treeOne);
	            }
	        }
	        return trees;
	    }

	    @Override
	    public List<SysOrganization> selectTreeGrid() {
	        return organizationMapper.selectAll();
	    }

}